import 'package:base_flutter/base_flutter.dart';
import 'package:fan_da_app/account/account_manager.dart';
import 'package:fan_da_app/net/apis.dart';
import 'package:fan_da_app/net/response/user_data.dart';

class UserModel extends BaseMvvmModel{
  @override
  void onCleared() {
  }

  ///用户登录
  Future<String> login({required String username,required String password}) async {
    var map = {
      "username":username,
      "password":password
    };
    var data = await HttpGo.instance.postData(Apis.login,data: map);
    if(200==data['code']){
      var token =  data['token'];
      await AccountManager.instance.saveToken(token);
      return token;
    }else{
      throw data['msg'];
    }
  }

  ///用户注册
  Future<String> register({required String username,required String password,required String nickName}) async {
    var map = {
      "username":username,
      "password":password,
      "nickName":nickName,
    };
    var data = await HttpGo.instance.postData(Apis.register,data: map);
    if(200==data['code']){
      var token =  data['token'];
      // await AccountManager.instance.saveToken(token);
      return token;
    }else{
      throw data['msg'];
    }
  }

  ///获取用户信息
  Future<UserData> userInfo() async {
    var data = await HttpGo.instance.getData(Apis.userInfo);
    if(200==data['code']){
      return UserData.fromJson(data['data']);
    }else{
      throw data['msg'];
    }
  }

}